<?php
/*
 * [佳喜网络!] (C)2008-2009 JiaXi Inc.
 * $Id: db.php 2008-8-13 19:28:02z pepsi.zh $
 * 数据库操作类
 * -------------------------------------------
 * 网址：www.he88.cn
 * -------------------------------------------
 * 这是一个共享软件，遵循我们公司的共享协议。
*/

if (!defined('IN_SITE')){ die('Hacking attempt!'); } 
class DbConn{
	public $mHost;					// hostname或服务器ip
	public $mUser;					// 登录数据库的用户名
	public $mPassword;				// 数据库密码
	public $mName;					// 数据库名
	public $mConnId;
	public $mQueryId;
	public $Debug = false;			// 是否显示sql语句 for debug； 
	public $Auto_Free = true;
	public $Show_Error = true;
	
	/* public: current error number and error text */
	private $Errno    = 0;
	private $Error    = "";
	
	## 连接数据库
	function Dbconn($host,$user,$password,$table){
		$this->mHost = $host;
		$this->mUser = $user;
		$this->mPassword = $password;
		$this->mName = $table;
		if (empty($this->mConnId)){
			$this->mConnId = @mysql_pconnect($this->mHost,$this->mUser,$this->mPassword);
			if (!$this->mConnId){
				$this->halt("pconnect($this->mHost, $this->mUser, *****) failed.");
			}
		}
		@mysql_select_db($this->mName);
	}
	
	## 执行mysql_query语句
	function Query($query){
		/* No empty queries, please, since PHP4 chokes on them. */
		/* The empty query string is passed on from the constructor,
         * when calling the class without a query, e.g. in situations
         * like these: '$db = new DB_Sql_Subclass;'
       */
		if (empty($query)){
			return 0;
		}
		 /* we already complained in connect() about that. */
		if (empty($this->mConnId)){
			return 0;
		}
    	// New query, discard previous result.
    	if ($this->mQueryId){
    		$this->freeQuery();
    	}
		if ($this->Debug){
			printf("Debug: Query = %s<br />\n",$query);
		}
		$this->mQueryId = @mysql_query($query,$this->mConnId);
		$this->Errno = mysql_errno();
		$this->Error = mysql_error();
		if (empty($this->mQueryId)){
			$this->halt("Invalid SQL: ".$query);
		}
		return $this->mQueryId;
	}
	
	## public: walk result set
	function nextRecord($Queryid=''){
		if ($Queryid){
			$row = @mysql_fetch_array($Queryid);
		}else {
			if (empty($this->mQueryId)) {
	    		$this->halt("nextRecord called with no query pending.");
	    		return 0;
	    	}
			$row = @mysql_fetch_array($this->mQueryId);
		}
		$this->Errno = @mysql_errno();
		$this->Error = @mysql_error();
		return $row;
	}

	## 删除记录返回 影响条数
	function queryDelete($query){
		$this->Query($query);
		return mysql_affected_rows();
	}
	
	## 添加数据
	function addData($data,$dbname){
		$i=1;
		if (!is_array($data)){
			$this->halt("数据 data 为非正确格式数组");
		}
		foreach($data as $key =>$value){
			if($i==1){
				$a_date_key = $key;
				$a_date_value = "'".$value."'";
			}else{
				$a_date_key .= ",".$key;
				$a_date_value .= ",'".$value."'";
			}
			$i++;
		}
		$query = "insert into ".$dbname." (".$a_date_key.") values (".$a_date_value.")";
		$this->Query($query);
		return $this->returnAffected();
	}

	## 更新数据
	function updateData($data,$dbname,$where){
		$i=1;
		$j=1;
		//分析数组结构
		if (is_array($where)){
			foreach($where as $key => $value){
				//unset($up_data[$key]);
				if($j==1){
					$up_limit = ' WHERE '.$key."='".$value."'";
				}else{
					$up_limit .= " AND ".$key."='".$value."'";
				}
				$j++;
			}
		}else {
			$up_limit = $where;
		}
		//分析数组结构
		foreach($data as $key =>$value){
			if($i==1){
				$up_date = $key."='".$value."'";
			}else{
				$up_date .= ",".$key."='".$value."'";
			}
			$i++;
		}
		$query = "UPDATE ".$dbname." SET ".$up_date.' '.$up_limit;
		return $this->Query($query);
	}
	
	## 返回字段的数目(很少用到)
	function getFieldsCount($Query){
		$this->mQueryId = $this->Query($Query);
		return @mysql_num_fields($this->mQueryId);
	}
	
	## 返回符合条件记录的条数
	function getRecordCount($Query){
		$this->mQueryId = $this->Query($Query);
		return @mysql_num_rows($this->mQueryId);
	}
	
	## 返回二维数组类型的数据
	function Fetcharray($Query,$result_type = MYSQL_ASSOC){
		$array = array();
		$this->mQueryId = $this->Query($Query);
		while(($row = @mysql_fetch_array($this->mQueryId,$result_type))){
			$array[] = $row;
		}
		if ($this->Auto_Free){
			$this->freeQuery();
		}
		return $array;
	}
	//返回分类，制作列表菜单
	function Fetcharray_area($Query,$id='id',$name='name'){
		if (empty($id) || empty($name)){
			$this->halt("id 或者 name 参数为空");
		}
		$this->mQueryId = $this->Query($Query);
		$array_area = array();
		while(($row = @mysql_fetch_array($this->mQueryId,MYSQL_ASSOC))){
			$array_area[$row[$id]] = $row[$name];
		}
		if ($this->Auto_Free){
			$this->freeQuery();
		}
		return $array_area;
	}
	## 返回数组类型的数据，并且带需要
	function Fetcharraynum($Query,$i=0,$result_type = MYSQL_ASSOC){
		$this->mQueryId = $this->Query($Query);
		$array = array();
		$i += 0;
		while(($row = @mysql_fetch_array($this->mQueryId,$result_type))){
			$i++;
			$row[num] = $i;
			$array[] = $row;
		}
		if ($this->Auto_Free){
			$this->freeQuery();
		}
		return $array;
	}
	function GetOneLineField($Query,$result_type = MYSQL_NUM){
		$array = array();
		$this->mQueryId = $this->Query($Query);
		while(($row = @mysql_fetch_array($this->mQueryId,$result_type))){
			$array[] = $row[0];
		}
		if ($this->Auto_Free){
			$this->freeQuery();
		}
		return $array;
	}
	#####  得到一个字段的数据
	function Getonefield($Query,$arraykey='',$result_type = MYSQL_ASSOC){
		$this->mQueryId = $this->Query($Query);
		$array = @mysql_fetch_array($this->mQueryId,$result_type);
		if (is_array($array)){
			if ($arraykey){
				$keyvalue = $array[$arraykey];
			}else {
				foreach ($array as $value){
					$keyvalue = $value;
				}
			}
		}
		return $keyvalue;
	}
	/////返回一维数组
	function Getonerecord($Query,$result_type = MYSQL_ASSOC){
		$this->mQueryId	= $this->Query($Query);
		$array = @mysql_fetch_array($this->mQueryId,$result_type);
		if ($this->Auto_Free){
			$this->freeQuery();
		}
		return $array;
	}
	#### 取插入数据时最后一个数据，即当前插入的数据
	function Insertid(){
		$id = @mysql_insert_id();
		return $id;
	}
	
	## 功能：返回INSERT，UPDATE 或 DELETE 查询 影响的条数
	function returnAffected($Queryid=''){
		if ($Queryid){
			$num = @mysql_affected_rows($Queryid);
		}else {
			$num = @mysql_affected_rows();
		}
		if ($num > 0){
			return $num;
		}else {
			return false;
		}
	}
	
	## 释放内存 public: discard the query result
	function freeQuery($Queryid='') {
		if ($Queryid){
			@mysql_free_result($Queryid);
		}
    	@mysql_free_result($this->mQueryId);
    	$this->mQueryId = 0;
	}
	
	## 输出错误
	private function halt($mesg){
		$this->Errno = @mysql_errno($this->mConnId);
		$this->Error = @mysql_error($this->mConnId);
		if($this->Show_Error){
			printf("</td></tr></table><b>Database error:</b> %s<br>\n", $mesg);
    		printf("<b>MySQL Error</b>: %s (%s)<br>\n",$this->Errno,$this->Error);
    		die("Session halted.");
		}
	}
}

###------------------------ 固定设置 ---------------------------###
## 数据表名
class DbTable{
	public $article				= "he88_article";
	public $site				= "he88_site";
	public $web					= "he88_web";
	public $belong				= "he88_belong";
	public $user				= "he88_user";
	public $userT				= "he88_usertype";
}
###------------------以下参数无需设置-------------------------###
$dbconn = new DbConn($mHost,$mUser,$mPassword,$mName);
$dbtable = new DbTable;

 ###------------------------ 结束设置 ---------------------------###
 ###mysql5.0设置数据库的字符集。。。
$dbconn->Query("SET NAMES 'utf8'");
?>